import {defineStore} from "pinia";
import {storyController} from '@/api/story/story.api'

interface StoryInfo {
  storyList : Array<Creator.Story>,
  storyConfigList: Array<Creator.StoryCreatorConfig>,
}
export const storyStore = defineStore('story-store', {
  state: (): StoryInfo => ({
    storyList: [],
    storyConfigList: [],
  }),
  getters: {
    getState(state: StoryInfo) {
      return state;
    },
    setState(state: StoryInfo) {
      return state;
    }
  },
  actions: {
    async fetchStoryInfo(storyId: string): Promise<void> {
      try {
        const res = await storyController.getStoryById(storyId);
        if (res.code === 200) {
          const index = this.storyList.findIndex((story) => story.id === storyId);
          if (index !== -1) {
            this.storyList[index] = res.data;
          } else {
            this.storyList.push(res.data);
          }
        } else {
          console.error("Failed to fetch story:", res.message);
        }
      } catch (error) {
        console.error("Error fetching story:", error);
      }
    },
    getStory(storyId: string): Creator.Story | undefined {
      return this.storyList.find((story) => story.id === storyId);
    }
  }
})
